package com.xykj.mythy.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RestController
@RequestMapping("/web/api/mail")
public class MailController {
    private static Logger LOG=LoggerFactory.getLogger(MailController.class);
    
    @Autowired
    private JavaMailSender mailSender;
     
    @Autowired
    private TemplateEngine templateEngine;
    /*
     * 发送普通邮件
     */
    @GetMapping("/sendMail")
    public String sendMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("349869496@qq.com");
        message.setTo("yangwencheng@ynkmxy.com");
        message.setSubject("邮件发送测试");
        message.setText("XY601产品验收通过");
        mailSender.send(message);
        LOG.info("发送成功!");
        return "发送成功！";
    }
    
    
    /*
     *  发送附件
     */
    @PostMapping("/sendAttachments")
    public String sendAttachmentsMail() throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("349869496@qq.com");
        helper.setTo("yangwencheng@ynkmxy.com");
        helper.setSubject("邮件发送带附件测试");
        helper.setText("SpringBoot send mail!");
        FileSystemResource file = new FileSystemResource(new File("D:/祥云灌区测站对应关系表.xlsx"));
        helper.addAttachment("祥云灌区测站对应关系表.xlsx", file);
        mailSender.send(mimeMessage);
        return "发送成功!";

    }
    
    /*
     * 发送文件
     */
    @PostMapping("/sendInlineMail")
    public String sendInlineMail() throws Exception {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("1310589391@qq.com");
        helper.setTo("349869496@qq.com");
        helper.setSubject("邮件发送测试");
        //这里的text 是html
        helper.setText("SpringBoot send mail!",true);
        FileSystemResource file = new FileSystemResource(new File("1.png"));
        helper.addInline("文件", file);
        mailSender.send(mimeMessage);
        return "发送成功!";
    }
    
    
    /*
     * 发送模板
     */
    @PostMapping("/sendTemplateMail")
    public void sendTemplateMail() throws Exception {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("1310589391@qq.com");
        helper.setTo("349869496@qq.com");
        helper.setSubject("邮件发送测试");
            
        //创建邮件正文
        Context context = new Context();
        context.setVariable("id", "1");
        context.setVariable("name", "YangWenCheng");
        String emailContent = templateEngine.process("emailTemplate", context);
        helper.setText(emailContent, true);
        mailSender.send(mimeMessage);
    }
    
}